#region License

/*
 * Copyright ?2002-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using Spring.Util;

namespace Spring.Expressions
{
    /// <summary>
    /// Represents NOT operator (both, bitwise and logical).
    /// </summary>
    /// <author>Aleksandar Seovic</author>
    [Serializable]
    public class OpNOT : UnaryOperator
    {
        /// <summary>
        /// Create a new instance
        /// </summary>
        public OpNOT() : base()
        {
        }

        /// <summary>
        /// Create a new instance
        /// </summary>
        public OpNOT(BaseNode operand)
            : base(operand)
        {
        }

        /// <summary>
        /// Create a new instance from SerializationInfo
        /// </summary>
        protected OpNOT(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// Returns a value for the logical NOT operator node.
        /// </summary>
        /// <param name="context">Context to evaluate expressions against.</param>
        /// <param name="evalContext">Current expression evaluation context.</param>
        /// <returns>Node's value.</returns>
        protected override object Get(object context, EvaluationContext evalContext)
        {
            object operand = GetValue(Operand, context, evalContext);
            TypeConverter convert = TypeDescriptor.GetConverter(operand.GetType());

            if (NumberUtils.IsInteger(operand))
            {
                return NumberUtils.BitwiseNot(operand);
            }
            else if (convert.CanConvertTo(typeof(int)))
            {
                operand = Convert.ToInt32(operand);
                return NumberUtils.BitwiseNot(operand);
            }
            else if (operand is Enum)
            {
                Type enumType = operand.GetType();
                Type integralType = Enum.GetUnderlyingType(enumType);
                operand = Convert.ChangeType(operand, integralType);
                object result = NumberUtils.BitwiseNot(operand);
                return Enum.ToObject(enumType, result);
            }
            else
                return !Convert.ToBoolean(operand);
        }
    }
}